from views import *


def edit_productor(request,  productor_id):
    """Create or edit a gift.  GET shows a blank form, POST processes it."""
    user = users.GetCurrentUser()
    if user is None:
      return http.HttpResponseForbidden('You must be signed in to add or edit a ')

    productor = None
    if productor_id:
      productor = Productor.get(db.Key.from_path(Productor.kind(), int(productor_id)))
      if productor is None:
        return http.HttpResponseNotFound('No productor exists with that key (%r)' %
                                           productor_id)

    form = ProductorForm(data=request.POST or None, instance=productor)

    if not request.POST:
      return respond(request, user, 'productor', {'form': form, 'productor': productor})

    errors = form.errors
    if not errors:
      try:
        product = form.save(commit=False)
      except ValueError, err:
        errors['__all__'] = unicode(err)
    if errors:
      return respond(request, user, 'product', {'form': form, 'product': product})

    product.key_name = product.name
    product.owner = user
    product.save()

    return http.HttpResponseRedirect('/')

def new_productor(request):
    """Create a new product"""
    return edit_productor(request, None)
